package com.hlt.controller.user;

import com.hlt.dto.SearchPageQueryDTO;
import com.hlt.entity.Categories;
import com.hlt.entity.Posts;
import com.hlt.entity.Users;
import com.hlt.result.Result;
import com.hlt.service.HomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home")
@Slf4j
@Api(tags = "首页接口")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 获取热门文章列表
     */
    @GetMapping("/hot-articles")
    @ApiOperation("获取热门文章列表")
    public Result<List<Posts>> getHotArticles() {
        log.info("获取热门文章列表");
        List<Posts> hotArticles = homeService.getHotArticles();
        return Result.success(hotArticles);
    }

    /**
     * 获取最新文章列表
     */
    @GetMapping("/latest-articles")
    @ApiOperation("获取最新文章列表")
    public Result<List<Posts>> getLatestArticles() {
        log.info("获取最新文章列表");
        List<Posts> latestArticles = homeService.getLatestArticles();
        return Result.success(latestArticles);
    }

    /**
     * 获取推荐文章列表
     */
    @GetMapping("/recommended-articles")
    @ApiOperation("获取推荐文章列表")
    public Result<List<Posts>> getRecommendedArticles() {
        log.info("获取推荐文章列表");
        List<Posts> recommendedArticles = homeService.getRecommendedArticles();
        return Result.success(recommendedArticles);
    }

    /**
     * 获取热门标签列表(分类表)
     */
    @GetMapping("/hot-tags")
    @ApiOperation("获取热门标签列表")
    public Result<List<Categories>> getHotTags() {
        log.info("获取热门标签列表");
        List<Categories> hotTags = homeService.getHotTags();
        return Result.success(hotTags);
    }

    /**
     * 获取热门作者列表
     */
    @GetMapping("/hot-authors")
    @ApiOperation("获取热门作者列表（活跃度）")
    public Result<List<Users>> getHotAuthors() {
        log.info("获取热门作者列表");
        List<Users> hotAuthors = homeService.getHotAuthors();
        return Result.success(hotAuthors);
    }

    /**
     * 获取分类列表
     */
    @GetMapping("/categories")
    @ApiOperation("获取分类列表")
    public Result<List<Categories>> getCategories() {
        log.info("获取分类列表");
        List<Categories> categories = homeService.getCategories();
        return Result.success(categories);
    }

    /**
     * 搜索文章
     */
    @PostMapping("/search")
    @ApiOperation("搜索文章")
    public Result<List<Posts>> searchArticles(@RequestBody SearchPageQueryDTO searchPageQueryDTO) {
        log.info("搜索文章，关键词：{}，页码：{}，每页数量：{}",
                searchPageQueryDTO.getKeyword(), searchPageQueryDTO.getPageNum(), searchPageQueryDTO.getPageSize());
        List<Posts> searchResults = homeService.searchArticles(searchPageQueryDTO);
        return Result.success(searchResults);
    }
}